<?php

abstract class MController extends CController
{
	//public $layout = '/layouts/main';
	public $head_js = array('js/jquery-1.8.0.min.js', 'js/jquery-ui.min.js');
	public $foot_js = array('js/common.js', 'js/plugins/ui/jquery.breadcrumbs.js', 'js/plugins/ui/jquery.tipsy.js');
	public $css = array('css/styles.css');
	public $var = array();
	public $util;
	public $module_id;
	public $controller_id;
	public $action_id;
	public $menu = array();
	
	public function __construct($id, $module = null)
	{
		parent::__construct($id, $module);
		$this->util =& Yii::app()->util;
		Yii::app()->params->module_rights = $this->module_rights();
	}
	
	/*
	public function filters() 
	{
		return array('AccessControl');
	}
	 
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('login', 'captcha', 'error'),
				'controllers' => array('index'),
				'users' => array('?')
			),
			array(
				'deny',
				'actions' => array(),
				'users' => array('?')
			)
		);
	}
	*/
	
	public function beforeAction($action)
	{
		if ($this->checkAccess())
		{
			if (! Yii::app()->user->isGuest)
			{
				$this->initMenu();
			}
			return true;
		}
		//return $this->checkAccess();
	}
	
	/**
	 * 验证权限
	 */
	public function checkAccess()
	{
		$this->module_id = $module = !$this->module ? '' : $this->module->id;
		$this->controller_id = $control = $this->id;
		$this->action_id = $action = $this->action->id;
		
		$rights = $this->module ? $this->module->params->rights : Yii::app()->params->rights;
		$public_rights =& $rights['public_rights'];
		
		//是否公用方法
		if (isset($public_rights[$control]) && 
				($public_rights[$control] == '*' || in_array($action, $public_rights[$control])))
		{
			return true;
		}
		
		if (!Yii::app()->user->isGuest)
		{
			$act_rights = Yii::app()->user->act_rights;
			//用户是拥有所有权限
			if ($act_rights == '*')
			{
				return true;
			}
			if ($this->module)
			{
				if (isset($act_rights[$module][$control]) &&
					($act_rights[$module][$control] == '*' || in_array($action, $act_rights[$module][$control])))
				{
					return true;
				}
			}
			else
			{
				//登录用户是否有权限
				if (isset($act_rights[$control]) &&
				($act_rights[$control] == '*' || in_array($action, $act_rights[$control])))
				{
					return true;
				}
			}
		}
		//Yii::app()->user->setFlash('error', '错误提示');
		$this->redirect(BASE_URL . 'login');
		
	}
	
	/**
	 * 验证栏目权限
	 * @param mixed $rights
	 * @param string $controller 控制器id
	 * @param string $action 动作id
	 * @param string $module 模块id
	 */
	public function checkRights($rights, $control = null, $action = null, $module = null)
	{
		if ($rights == '*') return true;
		
		if ($module)
		{
			if (! isset($rights[$module])) return false;
			if ($rights[$module] == '*') return true;
			if (! $control) return true;
			if (! isset($rights[$module][$control])) return false;
			if ($rights[$module][$control] == '*') return true;
			if (! $action) return true;
			if (in_array($action, $rights[$module][$control])) return true;
			
		}
		else 
		{
			if (! isset($rights[$control])) return false;
			if ($rights[$control] == '*') return true;
			if (! $action) return true;
			if (in_array($action, $rights[$control])) return true;
		}
	}
	
	/**
	 * @param string $module_id
	 * @return array
	 */
	public function module_rights($module_id = '')
	{
		$rights = array();
		if ($module_id == '')
		{
			foreach (Yii::app()->modules as $id => $item)
			{
				$module = Yii::app()->getModule($id);
				$rights[$id] = require $module->basePath . '/config/rights.php';
			}
		}
		else 
		{
			$module = Yii::app()->getModule($module_id);
			$rights = require $module->basePath . '/config/rights.php';
		}
		return $rights;
	}
	
	public function renderData($view, $return = false)
	{
		return $this->render($view, $this->var, $return);
	}
	
	/**
	 * 实现CActiveRecord::model()方法，5.3也以下可用
	 * @param string $name
	 * @return class CActiveRecord
	 */
	public function model($name)
	{
		return call_user_func(array($name, 'model'), $name);
	}
	
	public function initMenu()
	{
		//系统自带权限
		$rights = Yii::app()->params->rights['user_rights'];
		//模块权限
		$module_rights = Yii::app()->params->module_rights;
		//用户权限
		$act_rights = Yii::app()->user->act_rights;
		
		//左边栏
		foreach ($rights as $control => $item)
		{
			if (isset($item['hide']) || !$this->checkRights($act_rights, $control)) continue;
			$this->menu['left'][$control] = array(
				'text' => $item['text'],
				'url' => BASE_URL . $control . '/' . key($item['items']),
				'active' => (!$this->module && $control == $this->controller_id) ? 'class="active"' : '',
				'icon' => (!empty($item['icon'])) ? $item['icon'] : 'dashboard.png',
			);
		}
		
		foreach ($module_rights as $module => $item)
		{
			if (! $this->checkRights($act_rights, null, null, $module)) continue;
			$c_url = key($item['user_rights']);
			$a_url = current($item['user_rights']);
			$a_url = key($a_url['items']);
			$this->menu['left'][$module] = array(
				'text' => Yii::app()->getModule($module)->name,
				'url' => BASE_URL . $module . '/' . $c_url . '/' . $a_url,
				'active' => ($module == $this->module_id) ? 'class="active"' : '',
				'icon' => (!empty($item['icon'])) ? $item['icon'] : 'dashboard.png',
			);
		}
		
		//中间栏
		if (! $this->module)
		{
			if (isset($rights[$this->controller_id]))
			{
				foreach ($rights[$this->controller_id]['items'] as $action => $item)
				{
					if (isset($item['hide']) || !$this->checkRights($act_rights, $this->controller_id, $action)) continue;
					$key = $this->controller_id . '/' . $action;
					$this->menu['center'][$key] = array(
						'text' => $item['text'],
						'url' => BASE_URL . $this->controller_id . '/' . $action,
						'active' => ($action == $this->action_id) ? 'class="activeli"' : '',
						'a_active' => ($action == $this->action_id) ? 'class="this"' : ''
					);
				}
			}
		}
		else 
		{
			foreach ($module_rights[$this->module_id]['user_rights'] as $control => $item)
			{
				if (isset($item['hide']) || !$this->checkRights($act_rights, $control, null, $this->module_id)) continue;
				$key = $this->module_id . '/' . $control;
				$this->menu['center'][$key] = array(
					'text' => $item['text'],
					'active' => ($control == $this->controller_id) ? 'class="activeli"' : '',
					'a_active' => ($control == $this->controller_id) ? 'class="this"' : '',
					'ul_hide' => ($control != $this->controller_id) ? 'class="hide"' : ''
				);
				if (!empty($item['items']))
				{
					foreach ($item['items'] as $action => $value)
					{
						if (isset($value['hide']) || !$this->checkRights($act_rights, $control, $action, $this->module_id)) continue;
						$act_key = $key . '/' . $action;
						$this->menu['center'][$key]['items'][$act_key] = array(
							'text' => $value['text'],
							'url' => BASE_URL . $this->module_id . '/' . $control . '/' . $action,
							'active' => ($control == $this->controller_id && $action == $this->action_id) ? 'class="hover"' : '',
						);
					}
				}
			}
		}
		
		//右边导航栏
		$this->menu['nav']['index'] = array(
			'url' => BASE_URL,
			'text' => $rights['index']['text']
		);
		if (! $this->module)
		{
			$this->menu['nav']['control'] = $this->menu['common']['title'] = $rights[$this->controller_id]['text'];
			if (isset($rights[$this->controller_id]))
			{
				foreach ($rights[$this->controller_id]['items'] as $action => $item)
				{
					if (! $this->checkRights($act_rights, $this->controller_id, $action)) continue;
					if ($action == $this->action_id)
					{
						$this->menu['right']['current'] = array(
							'text' => $item['text'],
							'url' => BASE_URL . $this->controller_id . '/' . $action,
						);
						continue;
					}
					if (isset($item['hide']))
					{
						continue;
					}
					$this->menu['right']['child_items'][] = array(
						'text' => $item['text'],
						'url' => BASE_URL . $this->controller_id . '/' . $action,
					);
				}
			}
		}
		else
		{
			$m_rights = $module_rights[$this->module_id]['user_rights'];
			$this->menu['nav']['module'] = $this->menu['common']['title'] = Yii::app()->getModule($this->module_id)->name;
			foreach ($m_rights as $control => $item)
			{
				if (! $this->checkRights($act_rights, $this->controller_id, null, $this->module_id)) continue;
				if ($control == $this->controller_id)
				{
					continue;
				}
				if (isset($item['hide']))
				{
					continue;
				}
				$this->menu['right']['items'][] = array(
					'text' => $item['text'],
					'url' => BASE_URL .  $this->module_id . '/' . $control,
				);
			}
			
			if (isset($m_rights[$this->controller_id]))
			{
				$this->menu['nav']['control'] = $m_rights[$this->controller_id]['text'];
				foreach ($m_rights[$this->controller_id]['items'] as $action => $item)
				{
					if (! $this->checkRights($act_rights, $this->controller_id, $action, $this->module_id)) continue;
					if ($action == $this->action_id)
					{
						$this->menu['right']['current'] = array(
							'text' => $item['text'],
							'url' => BASE_URL . $this->module_id . '/' . $this->controller_id . '/' . $action,
						);
						continue;
					}
					if (isset($item['hide']))
					{
						continue;
					}
					$this->menu['right']['child_items'][] = array(
						'text' => $item['text'],
						'url' => BASE_URL .  $this->module_id . '/' . $this->controller_id . '/' . $action,
					);
				}
			}
		}
	}
}